<template>
	<view>
		<view class="input-group">
			<view class="flex-row input-view">
				<image class="input-left-img" src="../../static/img/user.png" mode="scaleToFill"></image>
				<view class="search-input">
					<input class="search-input" type="text" placeholder-class="input-placeholder" placeholder="请输入账号"
						v-model="userCode" :focus="isFocus" @confirm="checkUser">
				</view>
			</view>
			<view class="flex-row input-view" style="margin-top: 16rpx;">
				<image class="input-left-img" src="../../static/img/password.png" mode="scaleToFill"></image>
				<view class="search-input">
					<input class="search-input" type="password" placeholder-class="input-placeholder" placeholder="请输入密码"
						v-model="password">
				</view>
			</view>
		</view>
		<view class="submit-btn" @tap="handleCheckUser">
			<text>立即登录</text>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	var that;
	export default {
		data() {
			return {
				isFocus: true,     // 输入框焦点
				userCode: '',
				password: ''
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
			})  
		},
		onLoad() {
			that = this
		},
		onShow() {
			that.getServerData()
		},
		methods: {
			...mapMutations(['login', 'logout']),
			handleCheckUser() {
				if(that.userCode.trim() === '') {
					that.$common.showNormalToast('请输入您的账号！');
					return;
				}
				if(that.password.trim() === '') {
					that.$common.showNormalToast('请输入您的密码！');
					return;
				}
				that.checkUser()
			},
			checkUser() {
				let that = this;
				
				let requestData = {
					username: that.userCode,
					password: that.$common.MD5(this.password)					
				}
				uni.showLoading({
					title: '登录中'
				});
				that.$request.globalRequest('user/login', 'POST', requestData, '', 0,that.userCode).then(res => {	
					uni.hideLoading();
					//console.log(res)
					if(res.baseinfo) {
						if(res.baseinfo.token) {
							// 保存登录状态
							that.login(res.baseinfo);
							that.toMain();
						} else {
							that.$common.showNormalToast('账号或密码错误！');
						}
					}
				}).catch( res => {
					uni.hideLoading();
					that.$common.showNormalToast(res);					
				})
			},
			getServerData() {
				
			},
			toMain() {
				// 跳转到首页
				uni.reLaunch({
					url: '../index/index'
				})
			},
		}
	}
</script>

<style>
	@import url("lgoin.css");
</style>
